
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.jaxen.SimpleNamespaceContext;
import org.jaxen.XPath;
import org.jaxen.jdom.JDOMXPath;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * XMLReader.java
 *
 *
 * Created: Sun Dec 10 11:59:05 2006
 *
 * @author <a href="mailto:fseibel@sprintmail.com">Fred Seibel</a>
 * @version 1.0
 */
public class XMLReader  {
    Document doc;
    /**
     * Creates a new <code>XMLReader</code> instance.
     *
     * @param stream an <code>InputStream</code> value 
     */
    public XMLReader(InputStream stream, String kmlStr) {
	try {
		
		 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		    DocumentBuilder db = factory.newDocumentBuilder();
		    InputSource inStream = new InputSource();
		    inStream.setCharacterStream(new StringReader(kmlStr));
		    org.w3c.dom.Document docc =  db.parse(inStream);	
		     
		    NodeList nodeList = docc.getElementsByTagName("kml");
		   // System.out.println(docc.getNamespaceURI());
		   // System.out.println(" att length "+nodeList.item(0).getAttributes().getLength());
		    NamedNodeMap namemap=nodeList.item(0).getAttributes();
		    Node n = namemap.getNamedItem("xmlns");
		     String text = n.getNodeValue();
		    
		   // nodeList.item(0).getFeature(feature, version)
		    System.out.println("the node attribute value is "+text);
		    map.put( "edx", text);
		    
		getXML(stream);
	} catch (ParserConfigurationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SAXException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    }

 
    void getXML(InputStream stream) {
	try {
	    SAXBuilder builder = new SAXBuilder(false);
	    doc = builder.build(stream);
	 
	   	    
	} catch (JDOMException e) {
	    System.out.println(e + "\tXMLReader.java @ 60"); //Debug
	}
	catch (IOException e) {
	    System.out.println(e + "\tXMLReader.java @ 63"); //Debug
	}
    } // XMLReader constructor
    
    
    
    
    void getXML(InputStream strea, String stream) throws ParserConfigurationException, SAXException {
	try {
	    SAXBuilder builder = new SAXBuilder(false);
	    doc = builder.build(new StringReader(stream));
	  
	   	    
	} catch (JDOMException e) {
	    System.out.println(e + "\tXMLReader.java @ 60"); //Debug
	}
	catch (IOException e) {
	    System.out.println(e + "\tXMLReader.java @ 63"); //Debug
	}
    } // XMLReader constructor
    
    
    Document getDoc() {
    	return doc;
    }
    
   public String getString(String path) {
	try {
	    XPath xPath = new JDOMXPath(path);
	    xPath.setNamespaceContext( new SimpleNamespaceContext( map));
 	    Element elem = (Element) xPath.selectSingleNode(doc);
	    return elem.getValue().trim();
	}
	catch (Exception e) {
	    return "";
	}
	
    }
   
    HashMap<String, String> map = new HashMap<String, String> ();
    {
    	//<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
    	//map.put( "edx", "http://earth.google.com/kml/2.2");
    	
    		//map.put( "edx", "http://www.opengis.net/kml/2.2");
    }
    
    public List getNodes(String path) {
	try {
  
	    XPath xPath = new JDOMXPath( "//edx:kml/" +path);
	    xPath.setNamespaceContext( new SimpleNamespaceContext( map));
 	    List list = xPath.selectNodes(doc);
	    System.err.println(list + "\tXMLReader.java @ 155"); //Debug
	    return list;
	}
	catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}

    }

} // XMLReader
